<template>
    <div>
        <h3>房价计划修改</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">房价计划名称</td>
                    <td>
                        <input type="text" v-model="querinfo.HousePriceName">
                    </td>
                </tr>

                <tr>
                    <td align="right">门店</td>
                    <td>
                        <select v-model="querinfo.ShopId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in data.shoplist">{{ item.ShopName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">房型</td>
                    <td>
                        <select v-model="querinfo.HouseTypeId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in data.houselist">{{ item.HouseTypeName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">房型价格</td>
                    <td>
                        <input type="text" v-model="querinfo.Price">
                    </td>
                </tr>

                <tr>
                    <td align="right">房间数量</td>
                    <td>
                        <input type="text" v-model="querinfo.Count">
                    </td>
                </tr>

                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" name="State" v-model="querinfo.State" :value="1">开启
                        <input type="radio" name="State" v-model="querinfo.State" :value="0">禁用
                    </td>
                </tr>

                <tr>
                    <td align="right">早餐数量</td>
                    <td>
                        <input type="text" v-model="querinfo.BreakFastCount">
                    </td>
                </tr>

                <tr>
                    <td align="right">开始时间</td>
                    <td>
                        <input type="date" v-model="querinfo.BeginTime">
                    </td>
                </tr>

                <tr>
                    <td align="right">结束时间</td>
                    <td>
                        <input type="date" v-model="querinfo.EndTime">
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-primary" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, useSlots } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';
import axios from 'axios';
const route = useRoute();
const router = useRouter();

let querinfo: any = reactive({
    "Id":route.query.id,
    "HousePriceName": "",
    "ShopId": "",
    "HouseTypeId": "",
    "Price": "",
    "Count": "",
    "State": 1,
    "BreakFastCount": "",
    "BeginTime": "",
    "EndTime": "",
})

onMounted(() => {
    getList();
    GetHouse();
    Detail();
})
//反填
const Detail = () => {
    axios({
        method: "get",
        url: "/api/House/Detail",
        params: {
            id: route.query.id
        }
    })
        .then(res => {
            console.log(res)
            Object.assign(querinfo, res.data);
            querinfo.BeginTime = res.data.BeginTime.substring(0, 10);
            querinfo.EndTime = res.data.EndTime.substring(0, 10);
        })
}


//添加
const ok = () => {
    if (querinfo.HousePriceName == "") {
        alert('房型计划名称不能为空');
        return;
    }
    if (querinfo.ShopId == "") {
        alert('门店必选');
        return;
    }
    if (querinfo.HouseTypeId == "") {
        alert('房型必选');
        return;
    }
    let reg = /^[1-9]\d*$/  //正整数
    let reg1 = /^[1-9]\d*(\.\d+)?$/ //正小数
    if (querinfo.Price == "") {
        alert('房型价格不能为空');
        return;
    }
    if (!reg1.test(querinfo.Price)) {
        alert('房型价格只能是整数');
        return;
    }
    if (querinfo.Count == "") {
        alert('房间数量不能为空');
        return;
    }
    if (querinfo.BreakFastCount == "") {
        alert('早餐数量不能为空');
        return;
    }
    if (!reg.test(querinfo.BreakFastCount)) {
        alert('早餐数量必须是整数');
        return;
    }
    if (querinfo.BeginTime == "") {
        alert('开始时间不能为空');
        return;
    }
    if (querinfo.EndTime == "") {
        alert('结束时间不能为空');
        return;
    }
    

    axios({
        method: "post",
        url: "/api/House/UpdHousePrice",
        data: querinfo
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert('房型计划名称重复');
            }
            else if (res.data > 0) {
                alert('修改成功')
                router.push('/housepricelist')
            }
        })
        .catch(err => {
            console.log(err);
        })
}

let data: any = reactive({
    shoplist: [],
    houselist: []

})

//获取门店列表数据
const getList = () => {
    axios({
        method: "get",
        url: "/api/Shops/GetShopList",
        params: {
            pageSize: 100
        }
    })
        .then(res => {
            console.log(res);
            data.shoplist = res.data.list;
        })
        .catch(err => {
            console.log(err)
        })
}

//获取房型
const GetHouse = () => {
    axios({
        method: "get",
        url: "/api/House/GetHouseTypeList",
        params: {
            pageSize: 100
        }
    })
        .then(res => {
            console.log(res);
            data.houselist = res.data.list;
        })
        .catch(err => {
            console.log(err);
        })
}

</script>





<style scoped></style>